package ch.usi.inf.pf2.gui.chart;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ch.usi.inf.pf2.gui.JSpreadSheetGUI;

/**
 * 
 * @author Lorenzo Baracchi <lorenzo.baracchi@usi.ch>
 *
 */
public class JSpreadSheetChart 
{
    JFrame frame;
    JPanel panel;
    JSpreadSheetGUI spreadTable;
    
    
    /**
     * Create a window only for showing the chart upon the 
     * current spreadsheet.
     * @param spreadtable The table above the chart must be shown.
     */
    public JSpreadSheetChart (JSpreadSheetGUI spreadtable)
    {
	   this.spreadTable=spreadtable;
	   frame = new JFrame("Chose Chart");
	   frame.setLocationRelativeTo(spreadTable.getFrame());
	   panel= new JPanel();
	   panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
	   frame.getContentPane().add(panel);	   
	   makeFrame();
    }
    
    
    /**
     * Make the frame.
     */
    public void makeFrame()
    {
	   frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	   
	   String[] charts = {"Bar Chart", "Point Chart", "Line Chart", "Pie Chart"};
	   final JLabel hint= new JLabel("Show a bar chart \n for the selected values");
	   final JComboBox combo = new JComboBox(charts);
	   combo.addActionListener(new ActionListener()
	   {
	       @Override
	       public void actionPerformed(ActionEvent arg0)
	       {
	     	 String s = (String)combo.getSelectedItem();
	     	 if(s.equals("Bar Chart"))
	     		hint.setText("Show a bar chart \n for the selected values");
	     	 if(s.equals("Point Chart"))
	     		hint.setText("Show a scatter chart with points only \n for the selected values");
	     	 if(s.equals("Line Chart"))
	     		hint.setText("Show a scatter chart with points and lines \n for the selected values");
	     	 if(s.equals("Pie Chart"))
	     		hint.setText("Show a Pie chart \n for the selected values");
	     	 
	     	 frame.pack();
	       }
	   });
	   panel.add(combo);
	   panel.add(hint);
	   
	   final JButton ok = new JButton("Insert");
	   panel.add(ok);
	   ok.addActionListener(new ActionListener()
	   {
	       @Override
	       public void actionPerformed(ActionEvent e)
	       {
	   	   	String s = (String)combo.getSelectedItem();
	   	   	int heigth= 400;
	   	   	frame.dispose();
	   	   	JFrame frameChart = new JFrame("Chart"); 
	   	   	frameChart.setLocationRelativeTo(spreadTable.getFrame());
	   	   	
	   	   	if(s.equals("Bar Chart"))
	   	   	{
	   	   	
	   	   	    BarChart bc = new BarChart(spreadTable, spreadTable.getSelectedRows(), spreadTable.getSelectedColumns(), heigth);
	   	   	    frameChart.getContentPane().add(bc);
	   	   	    int width = bc.width();
	   	   	    frameChart.setSize(width, heigth);
	   	   	    frameChart.setVisible(true);
	   	   	}
	   	   	if(s.equals("Point Chart"))
	   	   	{
	   	   	    PointChart pc = new PointChart(spreadTable, spreadTable.getSelectedRows(), spreadTable.getSelectedColumns(), heigth);
	   	   	    frameChart.getContentPane().add(pc);
	   	   	    int width = pc.width();
	   	   	    frameChart.setSize(width, heigth);
	   	   	    frameChart.setVisible(true);
	   	   	}
	   	   	if(s.equals("Line Chart"))
	   	   	{
	   	   	    LineChart lc =new LineChart(spreadTable, spreadTable.getSelectedRows(), spreadTable.getSelectedColumns(), heigth);
	   	   	    frameChart.getContentPane().add(lc);
	   	   	    int width = lc.width();
	   	   	    frameChart.setSize(width, heigth);
	   	   	    frameChart.setVisible(true);
	   	   	}
	   	   	if(s.equals("Pie Chart"))
	   	   	{
	   	   	    PieChart pc = new PieChart(spreadTable, spreadTable.getSelectedRows(), spreadTable.getSelectedColumns(), heigth);
	   	   	    frameChart.getContentPane().add(pc);
	   	   	    int width = pc.width();
	   	   	    frameChart.setSize(width, heigth);
	   	   	    frameChart.setVisible(true);
	   	   	}
	       }
	   });
	   frame.pack();
	   frame.setVisible(true);
    }
}